/**
 * Program  : EvacuationMapActivity.java
 * Author   : long
 * Create   : 2016年5月6日 上午10:32:34
 *
 * Copyright 2016 by Embedded Internet Solutions Inc.,
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Embedded Internet Solutions Inc.("Confidential Information").  
 * You shall not disclose such Confidential Information and shall 
 * use it only in accordance with the terms of the license agreement 
 * you entered into with Embedded Internet Solutions Inc.
 *
 */
	
package cn.broadin.app.hotel.setup;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import cn.broadin.app.hotel.BaseActivity;
import cn.broadin.app.hotel.FireActivity;
import cn.broadin.app.hotel.R;
import cn.broadin.app.hotel.adapter.EvacuationMapAdapter;
import cn.broadin.app.hotel.app.AppConfig;
import cn.broadin.app.hotel.dialog.UpdateDialog;
import cn.broadin.app.hotel.task.EvacuationMapTask;
import cn.broadin.app.hotel.util.XHttpProxy;
import cn.broadin.app.hotel.util.XLog;
import cn.broadin.app.hotel.util.XToast;
import cn.broadin.app.hotel.util.XUIHelper;
import cn.broadin.app.hotel.util.XUtils;
import cn.broadin.app.hotel.view.EffectNoDrawBridge;
import cn.broadin.app.hotel.view.GridViewTV;
import cn.broadin.app.hotel.view.MainUpView;

/**
 * 疏散图列表选择界面
 * 
 * @author   long
 * @version  1.0.0
 * @date 2016年5月6日上午10:32:34
 */
public class EvacuationMapActivity extends BaseActivity{
	
	private GridViewTV mGridViewTV;
	
	private EvacuationMapAdapter mAdapter;
	
	private MainUpView mMainUpView;
	
	private String roomNum;
	
	private List<String> mapList;
	
	private View mOldView;
	
	private final static String SVAE_DIR = "/mnt/sdcard/";  //AMlogic芯片方案存放路径
	//private final static String SVAE_DIR = "/mnt/sdcard/";    // 非AMlogic芯片方案存放路径
	
	private UpdateDialog mConfirmDialog;
	
	public static final int MSG_SUCCESS = 1;
	
	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
				case MSG_SUCCESS:	
					XUIHelper.openMainActivity(EvacuationMapActivity.this);
					finish();
					break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_init_setp_02);
		
		mapList = new ArrayList<String>();
		mAdapter = new EvacuationMapAdapter(mapList, this);
		
		initView();
		loadEvacuationMapData();
	}
	
	/**
	 * 初始化界面
	 * 
	 * @author   long
	 * @return void
	 * @exception
	 * @author   long
	 * @since 1.0.0
	 * @date 2016年5月6日 上午10:57:57
	 */
	private void initView(){
		mMainUpView = (MainUpView) findViewById(R.id.mainUpView);
		mMainUpView.setUpRectResource(R.drawable.focus);
//		int rightOffset = (int) XUtils.getDimension(this, R.dimen.px45);
//		int bottomOffset = (int) XUtils.getDimension(this, R.dimen.px45);
//		mMainUpView.setDrawUpRectPadding(new Rect(0, 0, -, - bottomOffset));
		if (XUtils.getSDKVersion() == 17) {
			mMainUpView.setEffectBridge(new EffectNoDrawBridge()); 		// 4.3以下版本边框移动.
			EffectNoDrawBridge bridget = (EffectNoDrawBridge) mMainUpView.getEffectBridge();
			bridget.setTranDurAnimTime(200);
		} 
		
		mGridViewTV = (GridViewTV) this.findViewById(R.id.grid);
		mGridViewTV.setSelector(new ColorDrawable(Color.TRANSPARENT));
		mGridViewTV.setAdapter(mAdapter);
		mGridViewTV.setOnItemSelectedListener(new GridView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				if (view != null) {
					view.bringToFront();
					View childView = view.findViewById(R.id.evacuation_map);
					mMainUpView.setFocusView(childView, mOldView, 1.1f);
					mOldView = childView;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				
			}
		});
		mGridViewTV.setOnItemClickListener(new GridView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				
				final String url = mapList.get(position);
				final String fileName = System.currentTimeMillis() + ".jpg";
				
				// 当先图片做全屏查看
				mConfirmDialog = new UpdateDialog(EvacuationMapActivity.this);
				mConfirmDialog.setClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						new EvacuationMapTask(EvacuationMapActivity.this, new EvacuationMapTask.AddListener() {
							@Override
							public void onSuccess() {
								// 设置最新信息
								XUtils.setSharedPreferenceConfig(EvacuationMapActivity.this, FireActivity.MAP_KEY, SVAE_DIR + fileName);
								// 显示设置完成信息
								XToast.makeText(EvacuationMapActivity.this, "疏散图设置完成", 3000).show();
								// 跳转进入首页面
								mHandler.sendEmptyMessageDelayed(MSG_SUCCESS, 1000);
							}
						}).execute(new String[]{url, SVAE_DIR, fileName});
					}
				});
				mConfirmDialog.show();
			}
		});
//		mGridViewTV.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
//			@Override
//			public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
//				if (mGridViewTV.getChildCount() > 0) {
//					mGridViewTV.setSelection(0);
//					View newView = mGridViewTV.getChildAt(0);
//					newView.bringToFront();
//					View childView = newView.findViewById(R.id.evacuation_map);
//					mMainUpView.setFocusView(childView, 1.1f);
//					mOldView = childView;
//				}
//			}
//		});
	}
	
	@Override
	protected void onResume() {
		if(mOldView != null){
			mMainUpView.setFocusView(mOldView, 1.1f);
		}
		super.onResume();
	}
	
	/**
	 * 刷新表格数据
	 * 
	 * @author   long
	 * @return void
	 * @exception
	 * @author   long
	 * @since 1.0.0
	 * @date 2016年5月6日 上午10:57:14
	 */
	private void notifyGridViewDataChange(){
		mAdapter.setMapList(mapList);
		mAdapter.notifyDataSetChanged();
	}
	
	/**
	 * 加载服务器端疏散图数据列表
	 * 
	 * @author   long
	 * @return void
	 * @exception
	 * @author   long
	 * @since 1.0.0
	 * @date 2016年5月6日 上午10:57:35
	 */
	private void loadEvacuationMapData(){
		RequestParams requestParams = new RequestParams();
		requestParams.addBodyParameter("roomNum", roomNum);
		HttpUtils httpUtils = XHttpProxy.getHttpUtils();
		showLoadingDialog();
		httpUtils.send(HttpRequest.HttpMethod.POST, AppConfig.evacuationListUrl, requestParams, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException e, String msg) {
				hideLoadingDialog();
				XLog.e(msg);
				XToast.makeText(EvacuationMapActivity.this, "服务器繁忙，请稍后再试！").show();
			}

			@SuppressWarnings("unchecked")
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				XLog.i("responseInfo.result: " + responseInfo.result);
				mapList = (List<String>) JSON.parse(responseInfo.result);
				hideLoadingDialog();

				notifyGridViewDataChange();
			}
		});
	}
	
	/**
	 * @see android.app.Activity#onDestroy()
	 * @exception
	 * @author   long
	 * @since 1.0.0
	 * @date 2016年5月6日 下午4:33:06
	*/
	@Override
	protected void onDestroy() {
		// 解决Android Dialog 内存泄露的问题
		if(mConfirmDialog != null && mConfirmDialog.isShowing()){
			mConfirmDialog.dismiss();
		}
		mConfirmDialog = null;
		
		super.onDestroy();
	}
}
